package com.zhiyou100.api.object_demo;

import java.util.Comparator;
import java.util.Objects;

public class ObjectTest {

    public static void main(String[] args) {
        Student student = new Student("张三丰", 110, "20221014");
        //输出对象时，默认调用父类Object的toString，结果是内存地址
        System.out.println(student);
        //Object中的toString方法是不需要手动调用的
//        String s = student.toString();
//        System.out.println(s);
        //getClass    alt + enter
        Class<? extends Student> aClass = student.getClass();
        System.out.println("aClass = " + aClass);
        //获取当前class对象的全限定名
        String name = aClass.getName();
        System.out.println("name = " + name);
        //hashCode   获取当前对象的哈希码值   每个对象哈希值是唯一的
        int i = student.hashCode();
        System.out.println("i = " + i);
        //对象的比较
        Student student2 = new Student("张三丰", 108, "20221014");
        //默认equals比较的是两个对象的地址
        //此时我们可以根据需求重写equals方法
        System.out.println(student.equals(student2));

        //Objects   内部提供了大量的静态方法
        //equals 相对于Object中提供的equals更为安全
        boolean b = Objects.equals(student, student2);
        System.out.println("b = " + b);
        String s = "java";
        String x = new String("java");
        System.out.println(s == x);
        System.out.println("Objects.equals(s,x) = " + Objects.equals(s, x));
        //判断是否为空
        System.out.println("Objects.isNull(student2) = " + Objects.isNull(student2));
        //比较对象   compare
        int z = Objects.compare(student, student2, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        System.out.println("z = " + z);
    }
}